package com.newtribe.tk;

import java.util.HashMap ;
import java.util.Map;
/**
 * <p> </p>
 * <p>Description: Innovatation method</p>
 * <p>Copyright: Copyright (c)  newtribe innovation 2005</p>
 * <p> </p>
 * @author fuwei
 * @version 1.0
 */

public class ThreadPoolFactory {
  public ThreadPoolFactory() {
  }
  /**
   * create  a new ThreadPool contains TKWorker instances .
   * @param poolName
   * @param MAX
   * @return
   */
  public static ThreadPool createPool(String poolName, int MAX) throws Exception {
      return createPool(poolName ,5 ,MAX) ;
  }

  public static ThreadPool createPool(String poolName, int init, int MAX) throws Exception {
    if (poolCache.containsKey(poolName))
      throw new java.lang.IllegalStateException("pool has exist !");
    ThreadPool pool = new ThreadPool();
    pool.setMaxThreads(MAX);
    pool.setInitThreads(init);
    pool.setPoolName(poolName);
    poolCache.put(poolName, pool);
    pool.init(); //init it ;
    return pool;
  }


  private static Map poolCache = new HashMap() ;
  public static void main(String[] args) throws Exception  {    
    ThreadPool pool =ThreadPoolFactory.createPool("fuwei", 5) ;
    
    for(int i=0 ;i<100 ;i++) {

	   TKWorker worker =pool.findTKWorker()  ;
	   Task task =new TestTask();
	    worker.assignTask(task) ;
    }
    
    pool.stop();
  }


}

 class TestTask extends Task {
	  
	  public boolean execute () throws Exception {
		  	System.out.println(Thread.currentThread().getName());
		  	return true ;
		  } ;
}